package com.after.saas.system.easyexcel;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.after.saas.common.model.bo.OkxCoinBO;
import com.after.saas.common.model.bo.OkxMoneyBO;
import com.after.saas.system.converter.OkxConverter;
import com.after.saas.system.model.OkxCoin;
import com.after.saas.system.model.OkxMoney;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname EasyExceelUtil
 * @Description TODO
 * @auth after
 * @since 2023/9/15 22:07
 */


public class EasyExcelUtil {

    private final static HorizontalCellStyleStrategy HORIZONTAL_CELL_STYLE_STRATEGY;

    static {
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 内容字体
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short) 11);
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HORIZONTAL_CELL_STYLE_STRATEGY = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

    public static void exportCsv(List<OkxMoneyBO> datas) {
        File file = new File("D:\\test", "okxtest.csv");
        EasyExcel.write(file, OkxMoneyBO.class).excelType(ExcelTypeEnum.CSV).charset(StandardCharsets.UTF_8)
                .registerWriteHandler(HORIZONTAL_CELL_STYLE_STRATEGY)
                .autoCloseStream(true)
                .sheet().doWrite(datas);

    }

    public static void main(String[] args) {
        //1.设置写入文件夹地址和excel文件名称
//        String path = "D:\\test\\2023.09.14 23_14_38 ebc16fc0-cb51-43c3-a1ee-592d29d6e8b2.csv";
        String moneyPath = "D:\\test\\okx\\2022.10.27-2023.10.28-all.csv";
        String coinPath = "D:\\test\\okx\\coin-2022-10-27~2023-10-27.csv";
//        List<OkxMoneyBO> okxMoneyBOS = importCsv(moneyPath, OkxMoneyBO.class,1);
        List<OkxCoinBO> okxCoinBOS = importCsv(coinPath, OkxCoinBO.class, 2);
//        List<OkxMoney> moneyList = OkxConverter.INSTANCE.toOkxMoneyEntityList(okxMoneyBOS);
//        System.out.println("okxMoneyBOS\n"+JSONUtil.toJsonStr(moneyList));
//        List<OkxCoin> coinList = OkxConverter.INSTANCE.toOkxCoinEntityList(okxCoinBOS);
        exportCsv(okxCoinBOS, OkxCoinBO.class);
//        System.out.println("okxCoinBOS\n"+JSONUtil.toJsonStr(okxCoinBOS));
//        System.out.println("coinList\n" + JSONUtil.toJsonStr(coinList));
//        exportCsv(okexBOS);
    }

    public static <T> void exportCsv(List<T> dataList, Class<T> clazz) {
        String format = DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN);
        String fileName = String.format("%s.csv", format);
        File file = new File("D:\\test", fileName);
        EasyExcel.write(file, clazz).excelType(ExcelTypeEnum.CSV).charset(StandardCharsets.UTF_8)
                .registerWriteHandler(HORIZONTAL_CELL_STYLE_STRATEGY)
                .autoCloseStream(true)
                .sheet().doWrite(dataList);

    }

    public static <T> List<T> importCsv(String path, Class<T> clazz, int headNum) {
        // 实现excel写操作
        //调用easyExcel里面的方法实现写操作
        //2个参数，第一个参数是文件名称，第二个参数是实体类
        ExcelListener<T> listener = new ExcelListener<T>();
        EasyExcel.read(path, clazz, listener).charset(StandardCharsets.UTF_8).sheet().headRowNumber(headNum).doRead();
        return listener.getData();
    }
}
